package com.yangyang.sp04.order.service;

import com.yangyang.sp01.Service.OrderService;
import com.yangyang.sp01.pojo.Item;
import com.yangyang.sp01.pojo.Order;
import com.yangyang.sp01.pojo.User;
import com.yangyang.sp04.order.feign.ItemClient;
import com.yangyang.sp04.order.feign.UserClient;
import com.yangyang.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Override
    public Order getOrder(String orderId) {
        //TODO： 远程调用商品，获取商品列表
        JsonResult<List<Item>> items = itemClient.getItem(orderId);
        //todo： 远程调用用户，获取用户信息
        JsonResult<User> user = userClient.getUser(8);

        Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData() );
        order.setItems(items.getData());
        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单：order="+order);
        //TODO： 远程调用商品，减少库存
        itemClient.decreaseNumber(order.getItems());
        //TODO： 远程调用用户，增加积分
        userClient.addScore(order.getUser().getId(), 1000);

    }
}
